﻿using AutoMapper;
using MediatR;
using Production_Set.API.Application.Commands.Resourctes;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Domans.Produce.Resources;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Orderers
{
    /// <summary>
    /// 销售订单修改
    /// </summary>
    public class OrdersUpdateHandler : IRequestHandler<OrdersUpdateCommend, Result>
    {
        private readonly IBaseRepository<Orders> Orders;
        private readonly IMapper mapper;

        public OrdersUpdateHandler(IBaseRepository<Orders> Orders, IMapper mapper)
        {
            this.Orders = Orders;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(OrdersUpdateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Delete };

            var AddData = mapper.Map<Orders>(request);
            string sql = "UPDATE Orders SET OrdersNo=@OrdersNo ,OrdersName=@OrdersName ,OrdersTypeId=@OrdersTypeId ,ProjectId=@ProjectId " +
                         ",OrdersRemark=@OrdersRemark ,CustomerId=@CustomerId,OrdersPrice=@OrdersPrice," +
                         "Currency =@Currency ,PrePrice =@PrePrice,OrdersKTime =@OrdersKTime,OrdersJTime =@OrdersJTime" +
                         ",SignedTime =@SignedTime,OrdersState =@OrdersState,TaxType =@TaxType,Rate =@Rate" +
                         ",BillingPrice =@BillingPrice,CollectedPrice =@CollectedPrice,Salesman =@Salesman,DepartmentId =@DepartmentId" +
                         ",ApprovalStatusDD =0 WHERE OrdersId =@OrdersId";
            var list = Orders.Update(sql, new
            {
                OrdersId = AddData.OrdersId,
                OrdersNo = AddData.OrdersNo,
                OrdersName = AddData.OrdersName,
                OrdersTypeId = AddData.OrdersTypeId,
                ProjectId = AddData.ProjectId,
                OrdersRemark = AddData.OrdersRemark,
                CustomerId = AddData.CustomerId,
                OrdersPrice = request.OrdersPrice,
                Currency = request.Currency,
                PrePrice = request.PrePrice,
                OrdersKTime = request.OrdersKTime,
                OrdersJTime = request.OrdersJTime,
                SignedTime = request.SignedTime,
                OrdersState = request.OrdersState,
                TaxType = request.TaxType,
                Rate = request.Rate,
                BillingPrice = request.BillingPrice,
                CollectedPrice = request.CollectedPrice,
                Salesman = request.Salesman,
                DepartmentId = request.DepartmentId,
                ApprovalStatusDD = request.ApprovalStatusDD,

            });
            if (list != null)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "修改成功";
            }

            return results;
        }
    }
}
